-define(['jquery','jquery-weui','weui','common'], function ($,JqueryWuei,Weui) {
    var Controller = {
        index: function () {
            var storage_name = 'report_storage';
            var sid_value = '';
            var nickname_value = '';
            var group_value = '';

            // console.log(window.localStorage);
            // window.localStorage['sid'] = '';
            // window.localStorage['nickname'] = '';
            // window.localStorage['group_id'] = '';
            // window.localStorage.removeItem('sid');
            // window.localStorage.removeItem('nickname');
            // window.localStorage.removeItem('group_id');
            // console.log(window.localStorage['sid']);
            if(window.localStorage['sid'])
            {
                sid_value = window.localStorage['sid'];
            }
            else
            {
                if(window.localStorage[storage_name] ){
                    var localMsg ;
                    localMsg=JSON.parse(window.localStorage[storage_name]);
                    // console.log(localMsg);
                    var item =  Controller.api.findElem(localMsg, "id", "sid");
                    if(item != -1)
                    {
                        sid_value = item.value;
                    }
                }
            }

            if(window.localStorage['nickname'])
            {
                nickname_value = window.localStorage['nickname'];
            }
            else
            {
                if(window.localStorage[storage_name] ){
                    var localMsg ;
                    localMsg=JSON.parse(window.localStorage[storage_name]);
                    // console.log(localMsg);
                    var item =  Controller.api.findElem(localMsg, "id", "nickname");
                    if(item != -1)
                    {
                        nickname_value = item.value;
                    }
                }
            }

            if(window.localStorage['group_id'])
            {
                group_value = window.localStorage['group_id'];
            }
            else
            {
                if(window.localStorage[storage_name] ){
                    var localMsg ;
                    localMsg=JSON.parse(window.localStorage[storage_name]);
                    // console.log(localMsg);
                    var item =  Controller.api.findElem(localMsg, "name", "group_id");
                    if(item != -1)
                    {
                        group_value = item.value;
                    }
                }
            }
            $("#sid").val(sid_value);
            $("#nickname").val(nickname_value);

            if(group_value == '4')
                group_value = '2'

            $("input:radio[name='group_id'][value='" +group_value+ "']").prop("checked",true);

            $("#nickname").val(nickname_value);

            if(sid_value && nickname_value && group_value )
            {
                Controller.api.submit();
            }

            $('#formSubmitBtn').click(function () {
                // event.preventDefault();
                Controller.api.submit();
                return false;
            });

        },
        api: {
            submit: function () {
                Weui.form.validate('#form1', function (error) {
                    if (!error) {
                        // var data = {item_id: 'sid',item_type:'text',item_name:'row[sid]',item_value:$("#sid").val()};
                        // alert($("#sid").val());
                        window.localStorage['sid'] = $("#sid").val();
                        window.localStorage['nickname'] = $("#nickname").val();
                        window.localStorage['group_id'] = $("input[name='group_id']:checked").val();
                        console.log($("input[name='group_id']:checked").val());
                        $.ajax({
                            //几个参数需要注意一下
                            type: "POST",//方法类型
                            dataType: "json",//预期服务器返回的数据类型
                            url: "/index.php/index/trace" ,//url
                            data: $('#form1').serialize(),
                            success: function (ret) {
                                if(ret.code == 1)
                                {
                                    $.alert({
                                        title: '提示',
                                        text: ret.msg,
                                        // text: ret.msg + '<br><span style="color:red">请将此页面出示给相关工作人员作为进出凭证！</span><div id="timetext" style="font-size:24px;color:red;text-align:center"></div>',
                                        onOK: function () {
                                            WeixinJSBridge.invoke('closeWindow', {}, function (res) { });
                                        }
                                    });

                                    // $('#timetext').html('00:00:00:000');
                                    // var int;
                                    // int=setInterval(timer,100);
                                }
                                else if(ret.code == 0)
                                {
                                    $.alert({
                                        title: '提示',
                                        text:  ret.msg,
                                        onOK: function () {
                                            setbutton(1);
                                        }
                                    });
                                }

                            },
                            error : function(error) {
                                console.log(error);
                                alert(JSON.stringify(error))
                                $.alert({
                                    title: '提示',
                                    text: '网络异常,请再次提交！',
                                    onOK: function () {
                                        setbutton(1);
                                    }
                                });
                                // alert("异常！");
                            }
                        });
                    }

                });
            },
            findElem: function(arrayToSearch, attr, val)
            {
                for (var i = 0; i < arrayToSearch.length; i++) {
                // alert(arrayToSearch[i][attr]);
                // alert(val);
                    if (arrayToSearch[i][attr] == val) {
                      return arrayToSearch[i];
                    }
                }
                return -1;
            },
            getDate: function() {
                var date = new Date();
                //格式化为本地时间格式
                var date1 = date.toLocaleString();
                // return 'aaa';
                return date1;
            },
            // jishi: function() {
            //     var date = new Date();
            //     //格式化为本地时间格式
            //     var date1 = date.toLocaleString();
            //     $(".weui-dialog__bd").html(date1);
            // }
        }

    }
    return Controller;
})
